package com.weai.controller;

import com.weai.dto.ApiResponse;
import com.weai.dto.TaskOverviewDTO;
import com.weai.dto.TaskOverviewQueryDTO;
import com.weai.service.TaskOverviewService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 任务总览控制器
 */
@RestController
@RequestMapping("/api/tasks/overview")
@Tag(name = "任务总览", description = "任务总览相关操作接口")
@RequiredArgsConstructor
@Slf4j
public class TaskOverviewController {
    
    private final TaskOverviewService taskOverviewService;
    
    /**
     * 查询任务总览列表
     *
     * @param period 时间周期
     * @param taskType 任务类型
     * @param taskSubType 任务子类型
     * @param busiDepartment 业务部门
     * @param itDepartment IT部门
     * @param processor 处理人
     * @return 任务总览列表
     */
    @GetMapping
    @Operation(summary = "查询任务总览列表", description = "根据条件查询任务总览列表")
    public ApiResponse<List<TaskOverviewDTO>> getTaskOverviews(
            @Parameter(description = "时间周期") @RequestParam(required = false) String period,
            @Parameter(description = "任务类型") @RequestParam(required = false) String taskType,
            @Parameter(description = "任务子类型") @RequestParam(required = false) String taskSubType,
            @Parameter(description = "业务部门") @RequestParam(required = false) String busiDepartment,
            @Parameter(description = "IT部门") @RequestParam(required = false) String itDepartment,
            @Parameter(description = "处理人") @RequestParam(required = false) String processor) {
        
        // 构建查询参数
        TaskOverviewQueryDTO queryDTO = TaskOverviewQueryDTO.builder()
                .period(period)
                .taskType(taskType)
                .taskSubType(taskSubType)
                .busiDepartment(busiDepartment)
                .itDepartment(itDepartment)
                .processor(processor)
                .build();
        
        log.info("接收到查询任务总览列表请求, 条件: {}", queryDTO);
        List<TaskOverviewDTO> overviews = taskOverviewService.findTaskOverviews(queryDTO);
        return ApiResponse.success("查询成功", overviews);
    }
    
    /**
     * 更新任务总览信息
     *
     * @param id 任务ID
     * @param overviewDTO 任务总览信息
     * @return 更新后的任务总览信息
     */
    @PutMapping("/{id}")
    @Operation(summary = "更新任务总览信息", description = "根据ID更新任务总览信息")
    public ApiResponse<TaskOverviewDTO> updateTaskOverview(
            @Parameter(description = "任务ID", required = true) @PathVariable Integer id,
            @RequestBody TaskOverviewDTO overviewDTO) {
        
        log.info("接收到更新任务总览请求, ID: {}, 数据: {}", id, overviewDTO);
        overviewDTO.setId(id);
        TaskOverviewDTO updatedOverview = taskOverviewService.updateTaskOverview(overviewDTO);
        return ApiResponse.success("更新成功", updatedOverview);
    }
    
    /**
     * 删除任务
     *
     * @param id 任务ID
     * @return 操作结果
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除任务", description = "根据ID删除任务")
    public ApiResponse<Void> deleteTaskOverview(
            @Parameter(description = "任务ID", required = true) @PathVariable Integer id) {
        
        log.info("接收到删除任务请求, ID: {}", id);
        taskOverviewService.deleteTaskOverview(id);
        return ApiResponse.success("删除成功", null);
    }
} 